package com.yt.app.api.action;

import com.yt.app.api.action.base.BaseAction;
import com.yt.app.api.vo.AppTopicVo;
import com.yt.app.api.vo.BaseVo;
import com.yt.app.api.vo.IndexVo;
import com.yt.app.core.interfaces.AppBannerService;
import com.yt.app.core.interfaces.AppIconService;
import com.yt.app.core.interfaces.AppTopicFavService;
import com.yt.app.core.interfaces.AppTopicService;
import com.yt.app.core.model.AppBanner;
import com.yt.app.core.model.AppIcon;
import com.yt.app.core.model.AppTopic;
import com.yt.app.core.model.AppTopicFav;
import com.yt.app.core.utils.Constant;
import com.yt.core.MsgBean;
import com.yt.core.dao.filter.DynamicSQLFilterImpl;
import com.yt.core.dao.filter.QueryFilter;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@Api(tags = "06.话题")
@RequestMapping("/topic")
public class TopicController extends BaseAction{
    @Autowired
    private AppTopicService appTopicService;
    @Autowired
    private AppTopicFavService appTopicFavService;

    @ApiOperation(value="获取显示数据", httpMethod = "GET")
    @ApiResponses(value = {@ApiResponse(code = 200,message = "首页数据", response = AppTopic.class)})
    @RequestMapping("/get")
    public @ResponseBody  Object load(
            @ApiParam("话题ID") @RequestParam(value = "id", required = true) Long id){
        MsgBean msgBean = new MsgBean();
        AppTopic topic = appTopicService.get(id);
        if(topic == null){
            return msgBean.failure("话题不存在！").returnMsg();
        }
        appTopicService.increateViewNum(id);
        msgBean.put("data", topic);
        return msgBean.returnMsg();
    }

    @ApiOperation(value="收藏", httpMethod = "POST")
    @RequestMapping("/fav")
    public @ResponseBody  Object fav(
            @ApiParam("话题ID") @RequestParam(value = "topicId", required = true) Long topicId,
            @ApiIgnore HttpServletRequest request
            ){
        MsgBean msgBean = new MsgBean();
        AppTopic topic = appTopicService.get(topicId);
        if(topic == null){
            return msgBean.failure("话题不存在").returnMsg();
        }
        AppTopicFav fav = new AppTopicFav();
        fav.setTopicId(topicId);
        fav.setUid(BaseVo.getUIDFromHeader(request));
        fav.setDateline(System.currentTimeMillis()/1000);
        appTopicFavService.insertFav(fav);
        return msgBean.returnMsg();
    }

    @ApiOperation(value="分类话题", httpMethod = "GET")
    @ApiResponses(value = {
            @ApiResponse(code = 200,message = "话题", response = AppTopicVo.class)
    })
    @RequestMapping("/listByCatId")
    public @ResponseBody  Object topics(
            @ApiParam("上次最后一个ID,传递接口json中返回的lastId") @RequestParam(value = "lastId", required = false, defaultValue = "0") Long lastId,
            @ApiParam("分类ID") @RequestParam(value = "categoryId", required = true) Long categoryId){
        MsgBean msgBean = new MsgBean();
        QueryFilter filter = new DynamicSQLFilterImpl();
        if(lastId > 0) {
            filter.addFilter("Q_id_L_LT", String.valueOf(lastId));
        }
        filter.addFilter("Q_categoryId_L_EQ", String.valueOf(categoryId));
        filter.setSortField("id", true);
        List<AppTopic> appTopicList = appTopicService.find(filter);

        List<AppTopicVo> voList = new ArrayList<AppTopicVo>();
        for(AppTopic topic : appTopicList){
            voList.add(new AppTopicVo(topic));
        }

        if(appTopicList != null && !appTopicList.isEmpty()){
            msgBean.put("lastId", appTopicList.get(appTopicList.size()-1).getDateline());
        }else{
            msgBean.put("lastId", -1);
        }
        msgBean.put("topics", voList);

        return msgBean.returnMsg();
    }
}
